package edu.upc.dew.controller;

import edu.upc.dew.dto.JsonResponse;
import edu.upc.dew.entity.Category;
import edu.upc.dew.service.CategoryService;
import edu.upc.dew.service.exception.BusinessException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author gian.corzo
 */
@Controller("categoryController")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);

    @RequestMapping(value = "/categories",method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody JsonResponse getCategories() {

        boolean success = true;
        String errorMessage = null;
        List<Category> categories = null;

        try {
            categories = categoryService.getCategories();
        } catch (BusinessException be) {
            errorMessage = be.getMessage();
            success = false;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            errorMessage = e.getMessage();
            success = false;
        }
        return new JsonResponse(success,errorMessage,categories);
    }
}
